<?php
namespace App\Service\Member;

use Illuminate\Support\Facades\DB;

class IndexService extends BaseService
{
    //首页

    /***
     * 单子列表
     * @param array $pages
     * @param array $pas
     */
    public function gcList($pages, $pas)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbGc);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return $this->toback();
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $type = $v['type'];
            if ($type == 1) {
                $danzi = (array) $this->dbConn($this->dbDaiban)
                    ->where('id', $v['oid'])
                    ->first();
                $title = '代办';
                $yytime = empty($danzi) ? '-' : $danzi['yytime'];
            } else if ($type == 2) {
                $danzi = (array) $this->dbConn($this->dbYihu)
                    ->where('id', $v['oid'])
                    ->first();
                $title = empty($danzi) ? '小区医护' : $danzi['title'];
                $yytime = empty($danzi) ? '-' : $danzi['yytime'];
            } else {
                $danzi = (array) $this->dbConn($this->dbPeizhen)
                    ->where('id', $v['oid'])
                    ->first();
                $title = empty($danzi) ? '陪诊' : $danzi['title'];
                $yytime = empty($danzi) ? '-' : $danzi['start_time'];
                $danzi['uid']=empty($danzi) ? '-' : $danzi['pat_uid'];
            }
            $uid = empty($danzi) ? 0 : $danzi['uid'];
            $price = empty($danzi) ? 0 : $danzi['price'];
            $uname = '-'; $uhead = '';
            $sex_name = '-'; $age = 0;
            if (!empty($danzi)) {
            }
            $uOne = (array) $this->dbConn($this->dbUser)
                ->where('id', $uid)
                ->first();
            if (!empty($uOne)) {
                $uname = $uOne['uname'];
                $uhead = file_exists($uOne['head']) ? env('DOMAIN1').'/'.$uOne['head'] : '';
                $sex_name = $uOne['sex']==1 ? '男' : '女';
                $age = $uOne['age'];
            }
            $dOne = (array) $this->dbConn($this->dbDoctor)
                ->where('uid', $uid)
                ->first();
            $yiyuan_id = empty($dOne) ? 0 : $dOne['hospital'];
            $yyOne = (array) $this->dbConn($this->dbYiyuan)
                ->where('id', $yiyuan_id)
                ->first();
            $distance = '';
            if (!empty($yyOne) && $yyOne['map'] && isset($pas['locate']) && $pas['locate']) {
                $obj = new \App\Utilitys\TxMap();
                $res = $obj->mapByDistance($pas['locate'], $yyOne['map']);
                if ($res['code'] == 0) {
                    $distance = $res['data'];
                }
            }
            $arys[] = [
                'id' => $v['id'],
                'yw_type' => $v['type'],
                'yw_id' => $v['oid'],
                'title' => $title,
                'uid' => $uid,
                'uname' => $uname,
                'uhead' => $uhead,
                'price' => toNumber($price),
                'distance' => $distance,
                'sex_name' => $sex_name,
                'age' => $age.'岁',
                'yytime' => $yytime,
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

//    /**
//     * 小区医护+陪诊价格
//     */
//    public function unitPrice()
//    {
////        $res = (array) $this->dbConn($this->dbUdSeek)
////            ->where('del', 0)
////            ->where('doctor_id', 0)
////            ->where('seek_id', 0)
////            ->first();
//        $res = (array) $this->dbConn($this->dbConfig)
//            ->where('del', 0)
//            ->where('id', 1)
//            ->first();
//        if (empty($res)) {
//            return $this->toback();
//        }
//        $ary = [
//            'price' => toNumber($res['val']),
//        ];
//        return $this->toback(0, '', $ary);
//    }

    /**
     * 医院模糊搜索
     * @param string $key
     */
    public function yyListByKey($key)
    {
        $arys = [];
        $res = $this->dbConn($this->dbYiyuan)
            ->where('del', 0)
            ->where('title', 'like', $key.'%')
            ->get();
        if (!count($res)) {
            return $this->toback(0, '', $arys);
        }
        foreach ($res as $v) {
            $v = (array) $v;
            $arys[] = [
                'id' => $v['id'],
                'title' => $v['title'],
            ];
        }
        return $this->toback(0, '', $arys);
    }

    /**
     * 消息提醒列表
     * @param array $pages【start，limit】
     * @param int $uid
     */
    public function msgList($uid, $pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbUmsg)->where('uid', $uid);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return $this->toback();
        }
        $types = [1 => '提现到账'];
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $type_name = array_key_exists($v['type'], $types) ? $types[$v['type']] : '-';
            $arys[] = [
                'id' => $v['id'],
                'type' => $v['type'],
                'type_name' => $type_name,
                'msg' => $v['msg'],
                'addtime' => $v['addtime'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }
}